package fr.esiag.isidis.service;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fr.esiag.isidis.model.CDCDataItem;
import fr.esiag.isidis.model.WeightDataItem;
import fr.esiag.isidis.repository.CDCAndWeightRepository;

@Service
public class CDCAndWeightService {

	private static Logger logger = Logger.getLogger(CDCAndWeightService.class);

	@Autowired
	private CDCAndWeightRepository cdcAndWeightRepository;

	public void loadCsvFile() throws Exception
	{
		List<CDCDataItem> items = cdcAndWeightRepository.cdc_extractFromCsvFile();
		List<WeightDataItem> items2 = cdcAndWeightRepository.weight_extractFromCsvFile();

		Iterator<CDCDataItem> i = items.iterator();
		Iterator<WeightDataItem> j = items2.iterator();
		while(i.hasNext() && j.hasNext()){
			CDCDataItem x = (CDCDataItem) i.next();
			WeightDataItem y = (WeightDataItem) j.next();

			if (x.getDate().equals(y.getDate())){
				cdcAndWeightRepository.loadPdl(x,y);
			}
			logger.debug("Insertion de l'item " + items +("  et  ")+ items2 );
		}
	}
}
